// pages/index/index.js
import {formatDate,request} from "../../utils/utils";
import api from "../../utils/api";
Page({
  data: {
		navIndex:-1,
		nav:[],
		list:[],
		getTopPx:'',
		loadText:'正在加载中...'
  },
  onLoad (options) {},
  onReady () {
		this.page = 1;
		this.pageSize = 5;
		this.totalPage = 0;
		this.getTopPx();
		this.getNav();
    this.getList();
  },
	getTopPx(){
		let systemInfo = wx.getSystemInfoSync();
		let menuInfo = wx.getMenuButtonBoundingClientRect();
		let _barHeight = systemInfo.statusBarHeight;
		let _menuPad = menuInfo.top;
		let _menuHeight = menuInfo.height;
		let _topPx = _barHeight + _menuHeight + 2*( _menuPad - _barHeight);
		this.setData({ getTopPx:_topPx+'px' }) 					
	},
	navClick(e){
		if(e.target.dataset.index == this.data.navIndex){return}
		this.setData({
			navIndex : e.target.dataset.index,
			indexData:[],
      list:[],
		});
		this.getList();		
	},
	async getNav(){
		let nav = await request({url:api.getContentTags});
		this.setData({
			 nav:nav.data.data 
		});	
	},
  async getList(){
		this.page = 1;
		if(this.data.navIndex == -1){
			var params = {page:this.page,pageSize:this.pageSize}
		}else{
			var _tagId = this.data.nav[this.data.navIndex].id;
			var params = {page:this.page,pageSize:this.pageSize,tagId:_tagId}
		}
    let list = await request({url:api.getContentList, data:params});
		this.totalPage = list.data.totalPage;
		this.totalNum = list.data.totalNum;
		if(this.totalNum <= this.pageSize){
			this.setData({ loadText:'' }); 
		}else{
			this.setData({ loadText:'正在加载中...' }); 
		}
		this.setData({ list:list.data.data }); 
	},
	async onReachBottom(){

		if(this.page == this.totalPage){
			this.setData({ loadText:'没有新数据了' }); 
		}else{
			this.page++;
			let list = await request({url:api.getContentList, data:{page:this.page,pageSize:this.pageSize}});	
			let _list = this.data.list.concat(list.data.data);
			this.setData({ list:_list }); 
		}
	}
})